import LoadTopicPageDataMixin from "./LoadTopicPageDataMixin";
import {search} from "@/api/CommonApi";

/**
 * 用于TopicView组件的子路由页面
 */

export default {
    name:"TopicViewPageDataMixin",
    // 栏目code,通过路由配置props传递
    props:['code'],
    // 混入
    mixins:[
        // 加载栏目下的专辑分页数据
        LoadTopicPageDataMixin
    ],
    created(){
        // //设置栏目code
        // this.setPageDataCode(this.activeTopicCode.code);
        // //设置分页大小
        // this.setPageSize(12);
        // //加载分页数据
        // setTimeout(this.loadPageData(1).then( () => {
        //     console.log("12334666", this.pageData);
        // }));
    },
    computed:{
        // 当前激活的栏目的code
        activeTopicCode(){
            return {...this.$route.query}
        }
    },
    watch:{
        // 监听 当前激活的栏目的code 的变化
        activeTopicCode(val){
            if(val.keyword){
                search({
                    topicCode:val.code,
                    keyword:val.keyword,
                    searchTopic:true,   //是否搜索栏目
                    orderByClicks:'DESC',//按点击量降序排列
                    orderByCreateTime:'DESC' //按时间降序排列
                }).then(res => {
                    console.log("search res = ", res)
                    this.pageData = res.result.resource || {current: 1};
                    console.log("search = ", this.pageData)
                })
            }
            else if(val.code){
                // 如若传递的code值与当前页面配置的code值相同,return
                // if(val === this.loadPageDataSetting.code) return;
                this.loadPageDataSetting.code = val.code
                //如果有时间范围筛选
                if(val.timeRange){
                    this.setParams({timeRange : val.timeRange})
                    console.log("this.loadPageDataSetting = ", this.loadPageDataSetting)
                }
                // 否则重新加载数据
                this.loadPageData(1).then(() => {
                    console.log("topic = ", this.pageData);
                })
            }

        },
        // 监听路由变化,如果query.page参数发生改变,则重新拉取分页数据
        $route(to){
            // 页码变化
            let page = to.query.page
            if(!page) return;
            page = parseInt(page)
            if(page <= 0){return}
            if(page !== this.pageData.current){
                setTimeout(() => this.loadPageData(page))
                console.log("45678")
            }
        }
    }
}
